package com.open.rbac.service.id.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.open.rbac.model.entity.id.IdentityOrganizationRel;


@Transactional(rollbackFor = Exception.class)
public interface IdentityOrganizationRelRepository extends JpaRepository<IdentityOrganizationRel, String> {

    /**
     * 按Id物理删除'Id组织机构关系'
     */
    @Modifying
    @Transactional
    @Query("DELETE FROM identity_organization_rel rel WHERE rel.identityId = ?1")
    void deleteReallyByIdentityId(String identityId);

    @Modifying
    @Query("DELETE FROM identity_organization_rel rel WHERE rel.identityId IN (?1)")
    void deleteReallyByIdentityIds(List<String> identityIds);

    /**
     * 按组织机构Id物理删除'Id组织机构关系'
     */
    @Modifying
    @Transactional
    @Query("DELETE FROM identity_organization_rel rel WHERE rel.organizationId = ?1")
    void deleteReallyByOrganizationId(String organizationId);

    /**
     * 按组织机构Id列表查询Id组织机构关系列表
     */
    @Query("SELECT DISTINCT t1 FROM identity_organization_rel t1 WHERE t1.organizationId IN (?1)")
    List<IdentityOrganizationRel> findIdentityIds(List<String> organizationIds);


    @Query("SELECT DISTINCT t1 FROM identity_organization_rel t1 WHERE t1.identityId IN (?1)")
    List<IdentityOrganizationRel> findByIdentityIdIds(List<String> identityIds);

}
